package com.yh.workbill.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.yh.workbill.R;
import com.yh.workbill.base.BaseFragment;
import com.yh.workbill.databinding.FragmentMeBinding;
import com.yh.workbill.model.WorkBillCount;
import com.yh.workbill.ui.companyInfo.CompanyListInfoActivity;
import com.yh.workbill.ui.help.HelpActivity;
import com.yh.workbill.ui.leaseInfo.LeaseListInfoActivity;
import com.yh.workbill.ui.me.mvp.Me_Presenter;
import com.yh.workbill.ui.me.mvp.Me_View;

public class MeFragment extends BaseFragment implements Me_View {

    private MeViewModel viewModel;
    private Me_Presenter presenter;
    private FragmentMeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_me, container, false);

        return binding.getRoot();
    }

    @Override
    public void initJetPck() {
        viewModel = ViewModelProviders.of(this).get(MeViewModel.class);
    }

    @Override
    public void initSetting() {
        binding.setLifecycleOwner(getViewLifecycleOwner());
        presenter = new Me_Presenter(this);
        requireActivity().getLifecycle().addObserver(presenter);
    }

    @Override
    public void initObserver() {
        viewModel.getPhone().observe(requireActivity(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                binding.tvPhone.setText("登录账号:" + s);
            }
        });
        viewModel.getWorkBillCountData().observe(requireActivity(), new Observer<WorkBillCount>() {
            @Override
            public void onChanged(WorkBillCount data) {
                binding.tvBillworks.setText("" + data.getBillworks());
                binding.tvMoney.setText("" + data.getMoney());
            }
        });
    }

    @Override
    public void initDefaultMethod() {

    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getUserPhone();
        presenter.getWorkBillCount();
    }

    @Override
    public void initClick() {
        binding.llCompanyInfo.setOnClickListener(v -> {
            Intent i = new Intent(requireActivity(), CompanyListInfoActivity.class);
            startActivity(i);
        });
        binding.llUnitCompany.setOnClickListener(v -> {

        });
        binding.llHelp.setOnClickListener(v -> {
            Intent i = new Intent(requireActivity(), HelpActivity.class);
            startActivity(i);
        });
    }


    @Override
    public void initData() {
        binding.setData(new WorkBillCount(0, 0));
        binding.setPhone("");
    }

    @Override
    public void displayUserPhone(String phone) {
        viewModel.setPhone(phone);
    }

    @Override
    public void getWorkBillCountInfo(WorkBillCount data) {
        viewModel.setWorkBillCountData(data);
    }

    @Override
    public void showTasot(String message) {

    }

    @Override
    public void showDialog(String content, boolean isCancelable) {

    }

    @Override
    public void hideDialog() {

    }
}